/**
 * Created by zqk on 2018/4/24.
 */
var currentProductId = 0;
var productList;
var bt_list_edit = $('#bt_list_edit').attr('id');
var bt_list_add = $('#bt_list_add').attr('id');
var bt_list_delete = $('#bt_list_delete').attr('id');
$(document).ready(function () {
    var form=new xhForm($('#xh_form'));
    productList= $('#productList');
    $('.xh-datagrid-toolbar').parseToolbar();
    //设置禁用
    $('#bt_list_edit').setButtonDisable('eu');
    $('#bt_list_delete').setButtonDisable('eu');
    $('#bt_list_archive').setButtonDisable('eu');
    $('#testCase').setButtonDisable('eu');
    form.parse();
    form.bindRequired($('#bt_form_add,#bt_form_edit'));//绑定事件
    productList.datagrid({
        url: moduleUrl+'product/getProduct',
        method: 'post',
        rownumbers: true,
        pagination: true,
        pageSize: 50,
        pageList: [20,50,100,200],
        idField: 'iId',
        checkOnSelect: true,
        selectOnCheck: true,
        singleSelect: true,
        frozenColumns:[[
            {field:'ck',checkbox:true},
            {field:'iId',title:'iId',hidden:true},
            {field:'sName',title:'产品名称',width:100,align:'left',halign:'center'},
        ]],
        columns:[[
            {field:'iType',title:'应用对象',width:80,align:'center',halign:'center',
                formatter: function(value,row,index){
                    if(value){
                        return productTypes[value];
                    }
                }
            },
            {field:'sCode',title:'产品编号',width:150,align:'left',halign:'center'},
            {field:'sAndroPackage',title:'安卓包名',width:150,align:'left',halign:'center'},
            {field:'sAndroProject',title:'安卓项目',width:150,align:'left',halign:'center'},
            {field:'sIosPackage',title:'IOS包名',width:150,align:'left',halign:'center'},
            {field:'sIosEntPackage',title:'IOS包名(企业)',width:150,align:'left',halign:'center'},
            {field:'sServiceUrl',title:'服务地址',width:150,align:'left',halign:'center'},
            {field:'sDocUrl',title:'文档地址',width:150,align:'left',halign:'center'},
            {field:'tCreateDate',title:'创建时间',width:150,align:'center',halign:'center'},
            {field:'tLastModifyDate',title:'最后编辑时间',width:150,align:'center',halign:'center'},
        ]],
        onCheck:function (rowIndex, rowData) {
            if(currentProductId==rowData['iId']){
                currentProductId = 0;
                productList.datagrid('unselectRow',rowIndex);
                $('#bt_list_edit').setButtonDisable('eu');//编辑enable
                $('#bt_list_delete').setButtonDisable('eu');//删除enable
                $('#bt_list_archive').setButtonDisable('eu');//删除enable
                $('#testCase').setButtonDisable('eu');//删除enable
                return false;
            }else{
                currentProductId = rowData['iId'];
                $('#bt_list_edit').setButtonEnable('eu');//编辑enable
                $('#bt_list_delete').setButtonEnable('eu');//删除enable
                $('#bt_list_archive').setButtonEnable('eu');//删除enable
                $('#testCase').setButtonEnable('eu');//删除enable
            }
        },
    });

    $('#bt_list_search').click(function () {
        var bArchived = $('#bArchived').getElementValue();
        var productName = $('#productName').getElementValue();//获取input值
        var type = $('#productTypes').getElementValue();//获取input值
        productList.datagrid('load',{productName:productName,type:type,bArchived:bArchived});
    });



    $('.toolbar a').click(function () {
        var eleid = $(this).attr('id');
        var obj=productList.datagrid('getSelections')[0];
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        if(eleid=='bt_list_add'){
            window.top.xhTop.showModal({
                title:'新增产品',
                content: '<iframe src="'+moduleUrl+'product/productForm"  style="width: 100%;border-style: none;padding: 0px;height: 350px"  scrolling="yes"></iframe>',
                button:'提交',
                backClose: false
            });
        } else if(eleid =='bt_list_delete'&&obj!=undefined){
            myWindow.initPopUp({title: '确认删除', text: '确定要删除该产品吗?'},function () {
                $.ajax({
                    url:moduleUrl+'product/delProductById',
                    data:{iId:obj['iId']},
                    type:'POST',
                    success:function (data) {
                        window.top.xhTop.showSuccess(
                            data.msg
                        );
                        $('#bt_list_edit').setButtonDisable('eu');
                        $('#bt_list_delete').setButtonDisable('eu');
                        $('#bt_list_archive').setButtonDisable('eu');
                        $('#testCase').setButtonDisable('eu');
                        $('#bt_list_add').setButtonEnable('eu');
                        productList.datagrid('reload');
                    },
                    error:function (data) {
                        window.top.xhTop.showError(
                            data.responseJSON.error,
                            data.responseJSON.msg
                        );
                    }
                });
            })
        } else if(eleid=='testCase'&&obj!=undefined) {
            $.ajax({
                url:moduleUrl+'testcase/checkProductById',
                data:{iProductId:obj['iId']},
                type:'POST',
                success:function (data) {
                    var title = '产品 - '+obj['sName'];
                    window.top.xhTop.createNewTab(title,moduleUrl+'testcase/index?iProductId='+obj['iId'], true);
                },
                error:function (data) {
                    window.top.xhTop.showError(
                        data.responseJSON.error,
                        data.responseJSON.msg
                    );
                }
            });
        } else if(eleid =='bt_list_archive'&&obj!=undefined){
            if(xhExtra.buttonDisabled($(this))){
                return false;
            }
            myWindow.initPopUp({title: '归档确认', text: '确定要归档吗?'},function () {
                $.ajax({
                    url:moduleUrl+'product/archiveProductById',
                    data:{iId:obj['iId']},
                    type:'POST',
                    success:function (data) {
                        window.top.xhTop.showSuccess(data.msg);
                        $('#bt_list_edit').setButtonDisable('eu');
                        $('#bt_list_delete').setButtonDisable('eu');
                        $('#testCase').setButtonDisable('eu');
                        $('#bt_list_archive').setButtonDisable('eu');
                        $('#bt_list_add').setButtonEnable('eu');
                        productList.treegrid('reload');
                    },
                    error:function (data) {
                        window.top.xhTop.showError(
                            data.responseJSON.error,
                            data.responseJSON.msg
                        );
                    }
                });
            })
        }
    });

    $('#bt_list_edit').click(function () {  //编辑
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var obj=productList.datagrid('getSelections')[0];
        window.top.xhTop.showModal({
            title:'编辑项目',
            content: '<iframe src="'+moduleUrl+'product/productForm?iId='+obj['iId']+'"  style="width: 100%;border-style: none;padding: 0px;height: 350px"  scrolling="yes"></iframe>',
            button:'提交',
            backClose: false,
        });
    });
});
